package com.qianli.cashmama.product.server.facade;

import com.fqgj.common.api.Page;
import com.fqgj.common.api.Response;
import com.fqgj.common.api.enums.BasicErrorCodeEnum;
import com.fqgj.common.utils.CollectionUtils;
import com.google.common.collect.Lists;
import com.qianli.cashmama.product.facade.BusinessProductFacade;
import com.qianli.cashmama.product.facade.enums.PagePositionEnum;
import com.qianli.cashmama.product.facade.request.AppBusinessProductRequest;
import com.qianli.cashmama.product.facade.result.AppBusinessProduct;
import com.qianli.cashmama.product.facade.result.AppBusinessProducts;
import com.qianli.cashmama.product.facade.result.BusinessProduct;
import com.qianli.cashmama.product.server.dao.AppBusinessProductDao;
import com.qianli.cashmama.product.server.dao.BusinessProductDao;
import com.qianli.cashmama.product.server.entity.AppBusinessProductEntity;
import com.qianli.cashmama.product.server.entity.BusinessProductEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @program: cashmama-product
 * @description
 * @author: frank
 * @create: 2020-02-17 16:56
 **/
@Service("businessProductFacade")
public class BusinessProductFacadeImpl implements BusinessProductFacade {

    @Autowired
    private BusinessProductDao businessProductDao;
    @Autowired
    private AppBusinessProductDao appBusinessProductDao;

    @Override
    public Response<List<AppBusinessProduct>> getAppBusinessProductList(PagePositionEnum pagePositionEnum) {
        if(pagePositionEnum == null){
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "pagePositionEnum can not be empty");
        }
        List<AppBusinessProductEntity> appBusinessProductEntities = appBusinessProductDao.getAppBusinessProductListByPosition(pagePositionEnum.getPosistion());
        return Response.ok().putData(convertEntity2AppBusinessProduct(appBusinessProductEntities));
    }

    @Override
    public Response<List<AppBusinessProduct>> getAppBusinessProductList(PagePositionEnum pagePositionEnum, Long businessProductId) {
        if(pagePositionEnum == null || businessProductId == null || businessProductId <= 0){
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "param can not be empty");
        }
        List<AppBusinessProductEntity> appBusinessProductListByPositionAndId = appBusinessProductDao.getAppBusinessProductListByPositionAndId(pagePositionEnum.getPosistion(), businessProductId);
        return Response.ok().putData(convertEntity2AppBusinessProduct(appBusinessProductListByPositionAndId));
    }

    @Override
    public Response<List<AppBusinessProduct>> getAppBusinessProductList(AppBusinessProductRequest request) {
        if(request == null){
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "param can not be empty");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("position", request.getPosition());
        map.put("businessProductId", request.getBusinessProductId());
        map.put("buttonText", request.getButtonText());
        map.put("desc", request.getDesc());
        map.put("googleRating", request.getGoogleRating());
        map.put("icon", request.getIcon());
        map.put("id", request.getId());
        map.put("jumpUrl", request.getJumpUrl());
        map.put("name", request.getName());
        map.put("online", request.getOnline());
        map.put("position", request.getPosition());
        map.put("rank", request.getRank());
        map.put("reviews", request.getReviews());
        map.put("size", request.getSize());
        List<AppBusinessProductEntity> appBusinessProductEntities = appBusinessProductDao.selectByParams(map);
        if(CollectionUtils.isEmpty(appBusinessProductEntities)){
            return Response.ok().putData(null);
        }
        return Response.ok().putData(convertEntity2AppBusinessProduct(appBusinessProductEntities));
    }

    @Override
    public Response<AppBusinessProducts> getAppBusinessProductListByPage(PagePositionEnum pagePositionEnum, Page page) {
        Map<String, Object> paramMap = new HashMap();
        if(pagePositionEnum != null){
            paramMap.put("position", pagePositionEnum.getPosistion());
        }
        List<AppBusinessProductEntity> appBusinessProduct = appBusinessProductDao.getAppBusinessProductListByPage(paramMap, page);
        AppBusinessProducts appBusinessProducts = new AppBusinessProducts();
        appBusinessProducts.setAppBusinessProducts(convertEntity2AppBusinessProduct(appBusinessProduct));
        appBusinessProducts.setPage(page);
        return Response.ok().putData(appBusinessProducts);
    }

    private List<AppBusinessProduct> convertEntity2AppBusinessProduct(List<AppBusinessProductEntity> appBusinessProductEntities){
        List<AppBusinessProduct> products = Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(appBusinessProductEntities)){
            for(AppBusinessProductEntity entity : appBusinessProductEntities){
                AppBusinessProduct product = new AppBusinessProduct();
                BeanUtils.copyProperties(entity, product);
                products.add(product);
            }
        }
        return products;
    }

    @Override
    public Response<List<BusinessProduct>> getBusinessProductList() {
        List<BusinessProductEntity> businessProductList = businessProductDao.getBusinessProductList();
        List<BusinessProduct> businessProducts = Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(businessProductList)){
            for(BusinessProductEntity entity : businessProductList){
                BusinessProduct businessProduct = new BusinessProduct();
                BeanUtils.copyProperties(entity, businessProduct);
                businessProducts.add(businessProduct);
            }
        }
        return Response.ok().putData(businessProducts);
    }

    @Override
    public Response<Boolean> addAppBusinessProduct(AppBusinessProduct appBusinessProduct) {
        if(appBusinessProduct == null){
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "appBusinessProduct can not be empty");
        }
        if(appBusinessProduct.getId() == null && appBusinessProduct.getBusinessProductId() == null){
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "businessProductId can not be empty");
        }
        BusinessProductEntity businessProductEntity = businessProductDao.selectByPrimaryKey(appBusinessProduct.getBusinessProductId());
        if(businessProductEntity == null){
            return Response.error(BasicErrorCodeEnum.INTERNAL_SERVER_ERROR, "business product is empty");
        }

        if(appBusinessProduct.getId() != null){
            AppBusinessProductEntity entity = appBusinessProductDao.selectByPrimaryKey(appBusinessProduct.getId());
            if(entity == null){
                return Response.error(BasicErrorCodeEnum.INTERNAL_SERVER_ERROR, "product is not exist");
            }
            if(entity.getBusinessProductId() != appBusinessProduct.getBusinessProductId() || entity.getPosition() != appBusinessProduct.getPosition()){
                List<AppBusinessProductEntity> appBusinessProductEntities = appBusinessProductDao.getAppBusinessProductListByPositionAndId(appBusinessProduct.getPosition(), appBusinessProduct.getBusinessProductId());
                if(CollectionUtils.isNotEmpty(appBusinessProductEntities)){
                    return Response.error(BasicErrorCodeEnum.INTERNAL_SERVER_ERROR, "the position of product is existed");
                }
            }
            BeanUtils.copyProperties(appBusinessProduct, entity);
            BeanUtils.copyProperties(businessProductEntity, entity);
            entity.setId(appBusinessProduct.getId());
            appBusinessProductDao.updateByPrimaryKey(entity);
        }else{
            AppBusinessProductEntity entity = new AppBusinessProductEntity();
            BeanUtils.copyProperties(appBusinessProduct, entity);
            BeanUtils.copyProperties(businessProductEntity, entity);
            appBusinessProductDao.insert(entity);
        }
        return Response.ok().putData(true);
    }

    @Override
    public Response<Boolean> addBusinessProduct(BusinessProduct businessProduct) {
        if(businessProduct == null){
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "businessProduct can not be empty");
        }
        if(businessProduct.getId() != null){
            BusinessProductEntity entity = businessProductDao.selectByPrimaryKey(businessProduct.getId());
            BeanUtils.copyProperties(businessProduct, entity);
            businessProductDao.updateByPrimaryKey(entity);
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("businessProductId", businessProduct.getId());
            List<AppBusinessProductEntity> appBusinessProductList = appBusinessProductDao.getAppBusinessProductList(paramMap);
            if(CollectionUtils.isNotEmpty(appBusinessProductList)){
                for(AppBusinessProductEntity appBusinessProductEntity : appBusinessProductList){
                    appBusinessProductEntity.setButtonText(businessProduct.getButtonText());
                    appBusinessProductEntity.setDesc(businessProduct.getDesc());
                    appBusinessProductEntity.setJumpUrl(businessProduct.getJumpUrl());
                    appBusinessProductEntity.setIcon(businessProduct.getIcon());
                    appBusinessProductEntity.setName(businessProduct.getName());
                    appBusinessProductDao.updateByPrimaryKey(appBusinessProductEntity);
                }
            }
        }else{
            BusinessProductEntity entity = new BusinessProductEntity();
            BeanUtils.copyProperties(businessProduct, entity);
            businessProductDao.insert(entity);
        }
        return Response.ok().putData(true);
    }

    @Override
    public Response<Boolean> deleteBusinessProduct(Long id) {
        if(id == null || id <= 0){
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "id can not be empty");
        }
        businessProductDao.deleteByPrimaryKey(id);
        return Response.ok().putData(true);
    }
}
